package com.ltt.service.impl;

import com.ltt.dao.pojo.Chat;
import com.ltt.dao.pojo.FileItems;
import com.ltt.dao.pojo.User;
import com.ltt.mapper.ChatMapper;
import com.ltt.mapper.FileMapper;
import com.ltt.mapper.UserInfoMapper;
import com.ltt.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatMapper chatMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private FileMapper fileMapper;
    @Override
    public List<Chat> getChatListByGroupId(String groupId) {
        List<Chat> chats = chatMapper.getChatListByGroupId(groupId);
        for(Chat chat : chats) {
            User user = userInfoMapper.findUserById(chat.getInitiator());
            chat.setUser(user);
            FileItems fileParam = new FileItems();
            if(chat.getFileId() > 0){
                fileParam.setId(chat.getFileId());
                List<FileItems> files = fileMapper.findById(fileParam);
                chat.setFile(files.get(0));
            }
        }
        System.out.println(chats);
        return chats;
    }

    @Override
    public boolean setChat(Chat chat) {
        int res = chatMapper.setChat(chat);
        return res > 0;
    }

}
